<?php 
class UserModel extends BaseModel {
	public function add_user($data) {
		$result = D('Admin')->add($data);
		if($result){
			$return = $this->add_agent($result);
			if($return){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
		
	}

	public function get_user($aid) {
		$result = D('Admin')->where(array('aid'=>$aid))->find();
		return $result;
		
	}
	
	public function edit_pwd($aid,$pwd){
		$data['login_salt'] = rand(10000,99999);
		$data['pwd'] = md5(md5($pwd).$data['login_salt']);
		$result = D('admin')->where(array('aid'=>$aid))->save($data);
		return $result;
	}
	
	public function get_user_bygroup($gid) {
		$result = D('Admin')->where(array('group'=>$gid))->select();
		return $result;
		
	}

	public function edit_user($aid,$data) {
		$result = D('Admin')->where(array('aid'=>$aid))->save($data);
		if($result){
			if($data['group']==3){
				$return = $this->add_agent($aid);
				if($return){
					return true;
				}else{
					return false;
				}
			}else{
				return true;
			}
		}else{
			return false;
		}
		
	}

	public function del_user($id){
		$data['is_del']=1;
		$da = D('admin')->where(array('aid'=>$id))->save($data);
		if($da){
			return true;
		}else{
			return false;
		}
	}
	
	public function add_agent($aid){
		$agent = D('agent')->where('aid = '.$aid)->find();
		if(empty($agent)){
			$data['aid'] = $aid;
			if(D('agent')->add($data)){
				return true;
			}else{
				return false;
			}
		}else{
			return true;
		}
	}
	
	public function getagentlist(){
		$this->updatecompanynum();
		$users = D('admin')->where('`group` = 3')->select();
		foreach($users as $k => $v){
			$agentinfo = D('agent')->where('aid = '.$v['aid'])->find();
			if(!empty($agentinfo['area'])){
				$area = json_decode($agentinfo['area'],true);
				$agentinfo['area'] = array(
								'pro' => getareaname($area['pro']),
								'city' => getareaname($area['city'])
							);
			}
			$agent[] = array_merge($agentinfo,$v);
		}
		return $agent;
	}
	
	public function getagent($aid){
		$user = D('admin')->where('aid = '.$aid)->find();
		$agentinfo = D('agent')->where('aid = '.$aid)->find();
		if(!empty($agentinfo['area'])){
			$area = json_decode($agentinfo['area'],true);
			$agentinfo['area'] = array(
							'pro' => array('areaid'=>$area['pro'],'name'=>getareaname($area['pro'])),
							'city' => array('areaid'=>$area['city'],'name'=>getareaname($area['city']))
						);
		}
		$agent = array_merge($user,$agentinfo);
		return $agent;
	}
	public function editagent($data){
		$aid = $data['aid'];
		$data['area'] = json_encode(array('pro'=>$data['pro'],'city'=>$data['city']));
		$result = D('agent')->where('aid = '.$aid)->save($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
	public function updatecompanynum(){
		$agents = D('agent')->select();
		foreach($agents as $k => $v){
			$count = D('company')->where('company_agent = '.$v['aid'].' AND is_del = 0')->count();
			$data['company'] = $count;
			D('agent')->where('aid = '.$v['aid'])->save($data);
		}
	}
}
?>